package org.farng.mp3.object;

import java.util.Iterator;
import java.util.LinkedList;


/**
 * Title: Description: Copyright:    Copyright (c) 2002 Company:
 *
 * @author Eric Farng
 * @version $Revision: 1.7 $
 */
public class ObjectLyrics3Line extends AbstractMP3Object {
    /** DOCUMENT ME! */
    private LinkedList timeStamp = new LinkedList();

    /** DOCUMENT ME! */
    private String lyric = "";

    /**
     * Creates a new ObjectLyrics3Line object.
     *
     * @param identifier DOCUMENT ME!
     */
    public ObjectLyrics3Line(String identifier) {
        this.identifier = identifier;
    }

    public ObjectLyrics3Line(ObjectLyrics3Line copyObject) {
        super(copyObject);
        this.lyric = new String(copyObject.lyric);

        ObjectLyrics3TimeStamp newTimeStamp;

        for (int i = 0; i < copyObject.timeStamp.size(); i++) {
            newTimeStamp = new ObjectLyrics3TimeStamp((ObjectLyrics3TimeStamp) copyObject.timeStamp.get(i));
            this.timeStamp.add(newTimeStamp);
        }
    }

    public void setLyric(String lyric) {
        this.lyric = lyric;
    }

    public void setLyric(ObjectID3v2LyricLine line) {
        this.lyric = line.getText();
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getLyric() {
        return lyric;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        int size = 0;

        for (int i = 0; i < timeStamp.size(); i++) {
            size += ((ObjectLyrics3TimeStamp) timeStamp.get(i)).getSize();
        }

        return size + lyric.length();
    }

    /**
     * DOCUMENT ME!
     *
     * @param time DOCUMENT ME!
     */
    public void setTimeStamp(ObjectLyrics3TimeStamp time) {
        timeStamp.clear();
        timeStamp.add(time);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public Iterator getTimeStamp() {
        return timeStamp.iterator();
    }

    public void addLyric(String newLyric) {
        this.lyric += newLyric;
    }

    public void addLyric(ObjectID3v2LyricLine line) {
        this.lyric += line.getText();
    }

    /**
     * DOCUMENT ME!
     *
     * @param time DOCUMENT ME!
     */
    public void addTimeStamp(ObjectLyrics3TimeStamp time) {
        timeStamp.add(time);
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectLyrics3Line) == false) {
            return false;
        }

        ObjectLyrics3Line object = (ObjectLyrics3Line) obj;

        if (this.lyric.equals(object.lyric) == false) {
            return false;
        }

        if (this.timeStamp.equals(object.timeStamp) == false) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean hasTimeStamp() {
        if (timeStamp.isEmpty()) {
            return false;
        }

        return true;
    }

    /**
     * DOCUMENT ME!
     *
     * @param lineString DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readString(String lineString, int offset) {
        if (lineString == null) {
            throw new NullPointerException("Image is null");
        }

        if ((offset < 0) || (offset >= lineString.length())) {
            throw new IndexOutOfBoundsException("Offset to line is out of bounds: offset = " + offset + ", line.length()" + lineString.length());
        }

        int                    delim = 0;
        ObjectLyrics3TimeStamp time;
        timeStamp = new LinkedList();

        delim = lineString.indexOf("[", offset);

        while (delim >= 0) {
            offset = lineString.indexOf("]", delim) + 1;
            time   = new ObjectLyrics3TimeStamp("Time Stamp");
            time.readString(lineString.substring(delim, offset));
            timeStamp.add(time);
            delim = lineString.indexOf("[", offset);
        }

        lyric = lineString.substring(offset);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        String str = "";

        for (int i = 0; i < timeStamp.size(); i++) {
            str += timeStamp.get(i).toString();
        }

        return "timeStamp = " + str + ", lyric = " + lyric + "\n";
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String writeString() {
        String                 str = "";
        ObjectLyrics3TimeStamp time;

        for (int i = 0; i < timeStamp.size(); i++) {
            time = (ObjectLyrics3TimeStamp) timeStamp.get(i);
            str += time.writeString();
        }

        return str + lyric;
    }
}